<?php
session_start();
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/database.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/functions.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.admin.php');
require($_SERVER['DOCUMENT_ROOT'].'/resources/lib/class.user.php');

if(CRYPT_KEY=='') {
	exit("<script>alert('Please open ".$_SERVER['DOCUMENT_ROOT']."/config.php and enter and DEFINE CRYPT_KEY before continuing');</script>");	
	
}
if(DATABASE=='') {
	exit("<script>alert('Please open ".$_SERVER['DOCUMENT_ROOT']."/config.php and enter and DEFINE DATABSE before continuing');</script>");	
	
}

$mod=array('/inc/','/bundle/','/media/','/cache/','/themes/','/modules/');

foreach($mod as $m) {
	if(!chmod($_SERVER['DOCUMENT_ROOT'].$m,0777)) {
		exit("<script>alert('Please give the web user write access to ".$_SERVER['DOCUMENT_ROOT'].$m."');</script>");	
	}
}


$data=new database();
$data->db(DATABASE)->collection('settings');
$data->criteria(array('url'=>$_POST['site_url'],'name'=>$_POST['site_name'],'current_theme'=>'default','folder_path'=>'default'));
$_id=$data->insert();
$data->close();
/*"key" : "current_theme", "name" : "systemaddict", "folder_path" : "systemaddict" 
*/#
$data=new database();
$data->db(DATABASE)->collection('settings');
$data->criteria(array('key'=>'current_theme','name'=>'default','folder_path'=>'default'))->insert();
$data->close();

$data=new database();
$data->db(DATABASE);
$data->collection('settings')->criteria(array('key'=>1,'name'=>1,'folder_path'=>1,'url'=>1))->index();

$data->collection('uids')->criteria(array('uid'=>intval(rand(14000,15000))))->insert();
$data=new database();
$data->db(DATABASE);
$data->collection('uids')->criteria(array('uid'=>1))->index();

$data->collection('media')->criteria(array('path'=>1,'date'=>1))->index();

$data->collection('modules')->criteria(array('active'=>1))->index();

$data->collection('pages')->criteria(array('active'=>1,'cache'=>1,'is_blog'=>1,'is_blog_post'=>1,'is_parent'=>1,'order'=>1,'parent'=>1,'text'=>1,'url'=>1,'content'=>1, 'include_in_nav'=>1,'post_id'=>1))->index();

$data->collection('search_index')->criteria(array('page_id'=>1,'tags'=>1,'exerpt'=>1,'url'=>1,'heading'=>1))->index();

// comments will be here for blogs to index too
// make ther bundles


$admin=new admin();
$admin->createClientBundle(); // makes the Javascript and so on..

$user=new user();
$password=$user->encrypt($_POST['site_admin_password'],CRYPT_KEY);

$data->collection('users')->criteria(array(
'username'=>'Admin',
'password'=>$password,
'admin'=>intval(1),
'groups'=>array(),
'gravatar'=>'',
'email'=>$_POST['site_admin_email']))->insert();
$data->criteria(array('username'=>1,'groups'=>1))->index();
$data->close();
$data=new database();
$data->db(DATABASE)->collection('captcha');
$fp=file_get_contents($_SERVER['DOCUMENT_ROOT'].'/resources/lib/captcha-words.txt');
$words=trim($fp);
$words=explode("\n",$words);
$i=1;
foreach($words as $word) {
if(strlen($word)<=9) {
	$data->criteria(array('_id'=>new MongoId(),'word'=>$word,'rand'=>intval($i)))->insert();
}
$i++;
}
$data->close();

$data=new database();
$data->db(DATABASE)->collection('captcha');
$data->criteria(array('word'=>1,'rand'=>1))->index();
$data->close();
//file_put_contents($_SERVER['DOCUMENT_ROOT'].'/admin/installed.txt',"Installed on ".date("d/m/y G:i:s",time())." GMT");
//rename();
rename($_SERVER['DOCUMENT_ROOT'].'/admin/installer/install.php',$_SERVER['DOCUMENT_ROOT'].'/admin/installer/installed_'.md5(microtime(true)).'.php');
?>

<script>
alert('Site has been installed');
document.location.href='/login';
</script>
